package cn.wolfcode.shop.web.frontend;

import cn.wolfcode.shop.common.Result;
import cn.wolfcode.shop.domain.User;
import cn.wolfcode.shop.redis.RedisTemplate;
import cn.wolfcode.shop.redis.SeckillServerKeyPrefix;
import cn.wolfcode.shop.web.result.SeckillServerCodeMsg;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.UUID;

@RestController
public class PathController {

    @Autowired
    private RedisTemplate redisTemplate;

    @RequestMapping("/getPath")
    public Result<String> getPath(String uuid,User user,Integer result){
        if (user == null || result == null){
            return Result.error(SeckillServerCodeMsg.OP_ERROR);
        }
        Integer oldResult = redisTemplate.get(SeckillServerKeyPrefix.VERIFY_RESULT, uuid, Integer.class);
        if (oldResult == null){
            return Result.error(SeckillServerCodeMsg.VERIFYCODE_TIMEOUT);
        }
        if (oldResult-result != 0){
            return Result.error(SeckillServerCodeMsg.VERIFYCODE_ERROR);
        }
        String path = UUID.randomUUID().toString().replace("-", "");
        System.out.println("be..");
        redisTemplate.set(SeckillServerKeyPrefix.SECKILL_PATH,uuid,path);
        System.out.println("af");
        return Result.success(path);
    }
}
